<template>
    <div class="add-form">
        <el-form :model="dataForm" status-icon :rules="rules" ref="dataForm" label-width="100px">
            <el-form-item label="商品名称：" prop="name" label-suffix="hh">
                <el-input v-model="dataForm.name" style="width: 200px"></el-input>
            </el-form-item>
            <el-form-item label="商品类型：" prop="type">
                <el-select v-model="dataForm.type"
                           placeholder="请选择商品类型" style="width: 200px">
                    <el-option v-for="(category,index) in categories" :key="index"
                               :label="category.type+' ：'+category.name"
                               :value="category.type.toString()">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="商品价格：" prop="price">
                <el-input v-model="dataForm.price" style="width: 100px">
                    <template slot="suffix">￥</template>
                    <!--<template slot="append">￥</template>-->
                </el-input>
            </el-form-item>
            <el-form-item label="商品图片：" prop="url">
                <el-upload
                        class="avatar-uploader"
                        action="/api/uploadFile"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="dataForm.url" :src="dataForm.url" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="商品描述：" prop="description">
                <el-input type="textarea"
                          :rows="5"
                          placeholder="请输入商品描述"
                          v-model="dataForm.description"
                          style="width: 400px"></el-input>
            </el-form-item>
            <el-form-item label="商品库存：" prop="stock" style="width: 200px">
                <el-input v-model.number="dataForm.stock"></el-input>
            </el-form-item>
            <el-form-item label="商品评分：" prop="rating" style="width: 200px">
                <el-input v-model.number="dataForm.rating"></el-input>
            </el-form-item>
            <el-form-item label="商品状态：" prop="status">
                <el-radio-group v-model="dataForm.status">
                    <el-radio label="1">1:上架</el-radio>
                    <el-radio label="0">0：下架</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script type="text/ecmascript-6">
    export default {
        data() {
            let validateProductPrice = (rule, value, callback) => {
                if (/(^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*)|0)$)/.test(value)) {
                    callback()
                } else {
                    callback(new Error('请输入正数:最多两位小数'))
                }
            };
            return {
                dataForm: {
                    name: '',
                    type: '',
                    price: '',
                    url: '',
                    description: '',
                    stock: '',
                    status: ''
                },
                categories: [],
                rules:
                    {
                        name: [
                            {required: true, message: '商品名称不能为空', trigger: 'blur'}
                        ],
                        price:
                            [
                                {required: true, message: '商品价格不能为空', trigger: 'blur'},
                                {validator: validateProductPrice, trigger: ['blur', 'change']}
                            ],
                        type:
                            [
                                {required: true, message: '商品类型不能为空', trigger: 'blur'}
                            ],
                        url:
                            [
                                {required: true, message: '请为商品上传图片', trigger: 'blur'}
                            ],
                        description:
                            [
                                {required: true, message: '商品描述不能为空', trigger: 'blur'}
                            ],
                        stock:
                            [
                                {required: true, message: '库存不能为空', trigger: 'blur'},
                                {type: 'number', message: '请输入整数', trigger: ['blur', 'change']}
                            ],
                        rating:
                            [
                                {required: true, message: '评分不能为空', trigger: 'blur'},
                                {type: 'number', message: '请输入整数', trigger: ['blur', 'change']}
                            ],
                        status:
                            [
                                {required: true, message: '请选择药品状态', trigger: 'blur'}
                            ]
                    }
            }
        },
        methods: {
            submitForm() {
                this.$refs["dataForm"].validate((valid) => {
                    if (valid) {
                        this.$http.post("/api/product/save", {
                            name: this.dataForm.name,
                            type: this.dataForm.type,
                            price: this.dataForm.price,
                            rating: this.dataForm.rating,
                            old_price:'',
                            image: this.dataForm.url,
                            description: this.dataForm.description,
                            stock: this.dataForm.stock,
                            status: this.dataForm.status
                        }).then((response) => {
                            response=response.data
                            if (response.code === 0) {
                                this.$message({
                                    message: response.msg,
                                    type: 'success',
                                    customClass: 'alert-shadow'
                                });
                            } else {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    customClass: 'alert-shadow'
                                });
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            resetForm() {
                this.$refs["dataForm"].resetFields();
            },
            handleAvatarSuccess(res) {
                this.dataForm.url = res;
            },
            beforeAvatarUpload(file) {
                // const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 10;

                // if (!isJPG) {
                //     this.$message.error('上传头像图片只能是 JPG 格式!');
                // }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 10MB!');
                }
                return isLt2M;
            }
        },
        created() {
            this.$http.get("/api/categories").then((response) => {
                this.categories = response.data.records;
            });
        }
    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
